//
//  LAUMeiTuanRefreshView.swift
//  刷新控件的项目准备
//
//  Created by Lausen on 2017/5/7.
//  Copyright © 2017年 id. All rights reserved.
//

import UIKit

class LAUMeiTuanRefreshView: LAURefreshView{

    @IBOutlet weak var buildingIconView: UIImageView!
    @IBOutlet weak var earthIconView: UIImageView!
    @IBOutlet weak var kangarooIconView: UIImageView!
    
    /// 重写俯视图的高度
    override var parentViewHeight: CGFloat{
    
        didSet{
            
            if parentViewHeight < 23 {
                
                return
            }
            
            var scale: CGFloat
            
            
            if parentViewHeight > 126 {
                
                scale = 1
                
            }else{
                
                scale = 1 - (126 - parentViewHeight) / (126 - 23)
            }
            
            kangarooIconView.transform = CGAffineTransform(scaleX: scale, y: scale)
        }
    }
    
    override func awakeFromNib() {
        
        let bImage1 = UIImage(named: "icon_building_loading_1")
        let bImage2 = UIImage(named: "icon_building_loading_2")
        
        buildingIconView.image =  UIImage.animatedImage(with: [bImage1!,bImage2!], duration: 0.5)
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = -2 * M_PI
        anim.duration = 3
        anim.repeatCount = MAXFLOAT
        anim.isRemovedOnCompletion = false
        
        earthIconView.layer.add(anim, forKey: nil)
        
        /// 设置袋鼠的动画
        let kImage1 = #imageLiteral(resourceName: "icon_small_kangaroo_loading_1")
        let kImage2 = #imageLiteral(resourceName: "icon_small_kangaroo_loading_2")
        
        kangarooIconView.image =  UIImage.animatedImage(with: [kImage1,kImage2], duration: 0.5)
        
        /// 给袋鼠设置锚点
        kangarooIconView.layer.anchorPoint = CGPoint(x: 0.5, y: 1)
        
        let x = self.bounds.width * 0.5
        let y = self.bounds.height - 23
        
        kangarooIconView.center = CGPoint(x: x, y: y)
        
        kangarooIconView.transform = CGAffineTransform(scaleX: 0.2, y: 0.2)
    }
}
